package com.ricky.base;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Ricky
 * @date 2020/11/27
 */
@Slf4j
public class BaseApp {
    public static ConfigurableApplicationContext app;
    public static String host = "";
    public static String port = "";

    public static <T> void launch(Class<T> tClass, String[] args) throws UnknownHostException {
        SpringApplication springApplication = new SpringApplication(tClass);
        springApplication.setBannerMode(Banner.Mode.OFF);
        app = springApplication.run(args);
        Environment env = app.getEnvironment();
        host = InetAddress.getLocalHost().getHostAddress();
        port = env.getProperty("server.port");
        String appName = env.getProperty("spring.application.name");
        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:{}\n\t" +
                        "External: \thttp://{}:{}\n" +
                        "----------------------------------------------------------",
                appName, port, host, port);
    }
}
